#include "terminal.h"

void term_reset_input_mode(void)
{
    tcsetattr(STDIN_FILENO, TCSANOW, &saved_attributes);
}

void term_set_input_mode(void)
{
    struct termios tattr;

    /* Ter certeza de que é um terminal */
    if(!isatty(STDIN_FILENO))
    {
        fprintf(stderr, "Not a terminal\n");
        exit(EXIT_FAILURE);
    }

    /* Salva os atributos do terminal para que possamos recuperá-lo depois */
    tcgetattr(STDIN_FILENO, &saved_attributes);
    atexit(term_reset_input_mode);

    /* Seta o modo não-canônico */
    tcgetattr(STDIN_FILENO, &tattr);
    tattr.c_lflag &= ~(ICANON | ECHO); /* Limpa ICANON and ECHO */
    tattr.c_cc[VMIN] = 1;
    tattr.c_cc[VTIME] = 0;
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr);
}

void term_color(int attr, int fg)
{
    char command[13];
    /*sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);*/
    sprintf(command, "%c[%d;%dm", 0x1B, attr, fg + 30);
    if(write(STDOUT_FILENO, command, strlen(command)) == -1) perror("Error changing the terminal color\n");
}

string *read_line(void)
{
    string *line = str_new();
    char c = 0;

    while(1)
    {
        c = getchar();
        if(c == 127) term_do_backspace();
        else if(c == 9) term_do_tab();
        else if(c == 27)
        {
            c = getchar();
            if(c == 91)
            {
                c = getchar();
                if(c == 65) term_do_up_arrow();
                else if(c == 66) term_do_down_arrow();
                else if(c == 67) term_do_right_arrow();
                else if(c == 68) term_do_left_arrow();
                else if(c == 53)
                {
                    c = getchar();
                    if(c == 126) term_do_page_up();
                }
                else if(c == 54)
                {
                    c = getchar();
                    if(c == 126) term_do_page_down();
                }
                else if(c == 51)
                {
                    c = getchar();
                    if(c == 126) term_do_delete();
                }
                else if(c == 49)
                {
                    c = getchar();
                    if(c == 53 || c == 55 || c == 56 || c == 57) c = getchar();
                }
                else if(c == 50)
                {
                    c = getchar();
                    if(c == 48 || c == 52) c = getchar();
                }
            }
            else if(c == 79)
            {
                c = getchar();
                if(c == 72) term_do_home();
                else if(c == 70) term_do_end();
            }
        }
        else
        {
            if(isalnum(c) || ispunct(c) || isspace(c))
            {
                putchar(c);
                str_catc(line, c);
            }
        }
    }

    return line;
}

void term_do_backspace(void)
{
    printf("backspace ");
}

void term_do_up_arrow(void)
{
    printf("uparrow ");
}

void term_do_down_arrow(void)
{
    printf("downarrow ");
}

void term_do_left_arrow(void)
{
    printf("leftarrow ");
}

void term_do_right_arrow(void)
{
    printf("rightarrow ");
}

void term_do_delete(void)
{
    printf("delete ");
}

void term_do_tab(void)
{
    printf("tab ");
}

void term_do_home(void)
{
    printf("home ");
}

void term_do_end(void)
{
    printf("end ");
}

void term_do_page_up(void)
{
    printf("pageup ");
}

void term_do_page_down(void)
{
    printf("pagedown ");
}
